#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//https://www.nowcoder.com/questionTerminal/2c6a0a8e1d20492f92941400036e0890?f=discussion

int main()
{
    int n;
    int prevstr1 = 0, prevstr2 = 0;
    int result = 0x3f3f3f3f;
    string str1, str2, strs;

    cin >> n;
    cin >> str1 >> str2;

    while (n)
    {
        cin >> strs;
        if (strs == str1)
            prevstr1 = n;
        else if (strs == str2)
            prevstr2 = n;

        if (prevstr1 && prevstr2)
            result = min(result, abs(prevstr1 - prevstr2));
        n--;
    }

    cout << (prevstr1 && prevstr2 ? result : -1) << endl;

    return 0;
}
